Connecting to GitHub
Connecting to GitHub
Once you have a local Git repository, you can link it to GitHub so your work is backed up online and shareable with others.
What Does Connecting Mean?
A remote is a URL that points to a repository hosted somewhere else — in this case, GitHub. When you connect your local repo to GitHub, Git knows where to push commits and where to pull changes from.
Create a Repository on GitHub
-
Sign in to github.com.
-
Click the + icon in the top-right corner and select New repository.
-
Give the repository a name that matches your local project.
-
Leave Initialize this repository with a README unchecked — you already have a local repo.
-
Click Create repository.
GitHub will show you a page with setup instructions. Keep this page open.
Link Your Local Repo to GitHub
- In your terminal, navigate to your local project:
cd projects/git-demo
- Add the GitHub repository as a remote named
origin:
git remote add origin git@github.com:your-username/your-repo.git
- Verify the remote was added:
git remote -v
Output:
origin git@github.com:your-username/your-repo.git (fetch)
origin git@github.com:your-username/your-repo.git (push)
- Push your local commits to GitHub for the first time:
git push -u origin main
The -u flag sets origin/main as the upstream tracking branch. After this, you can run git push without arguments.
If your default branch is named master instead of main, replace main with master in the push command.
You can rename your local branch to main with: git branch -m master main
Common Mistakes
error: remote origin already exists — you've already added a remote called origin. Check it with git remote -v and update it if needed:
git remote set-url origin git@github.com:your-username/your-repo.git
Permission denied (publickey) — your SSH key isn't set up yet. See the SSH Keys lesson before continuing.
src refspec main does not match any — you haven't made any commits yet. Run git add . then git commit -m "Initial commit" first.
Next Steps: Adding a Remote with git remote add